import { MineModel } from '../../../models/mine'
const mineModel = new MineModel()
const App = getApp()
Component({
  /**
   * 组件的属性列表
   */
  externalClasses: ['ex-class'],
  options: {
    multipleSlots: true
  },
  properties: {
    text: { type: String, value: '请先绑定手机号' },
    visible: Boolean
  },
  /**
   * 组件的初始数据
   */
  data: {},
  /**
   * 组件的方法列表
   */
  methods: {
    //取消
    cancel: function() {
      this.setData({
        visible: false
      })
    },
    confirm: function() {
      this.setData({
        visible: false
      })
    },
    // 获取手机号
    getPhoneNumber: function(e) {
      const self = this
      App.WxService.login()
        .then(res => {
          return res.code
        })
        .then(code => {
          if (e.detail.errMsg == 'getPhoneNumber:ok') {
            const params = {
              code: code,
              encrypteData: e.detail.encryptedData,
              iv: e.detail.iv,
              regType: 3
            }
            mineModel
              .wx_bind_phone(params)
              .then(res => {
                wx.showToast({
                  title: '手机号绑定成功',
                  icon: 'none',
                  success: function() {
                    // 绑定成功更新 App.globalData.userInfo
                    App.updateUserInfo()
                  }
                })
              })
              .catch()
          } else {
            wx.showToast({
              title: '未授权',
              icon: 'none',
              success: function() {
                self.setData({
                  visible: false
                })
              }
            })
            return
          }
        })
    }
  }
})
